package com.sofwin.interceptor;

import com.sofwin.Constants;
import com.sofwin.pojo.SysUser;
import com.sofwin.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @packageName: com.sofwin.interceptor
 * @user: andyliu
 * @date: 2021/12/15 9:40
 * @email 115176513@qq.com
 * @description: TODO
 */

public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Subject subject = SecurityUtils.getSubject();//
        String loginName = subject.getPrincipal().toString();//获取用户名
        SysUser user = new SysUser();
        user.setLoginAccount(loginName);
        user = userService.checkLogin(user);
        if(user==null){
            response.sendRedirect(request.getContextPath()+"/index.html");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
